package com.timeDeal.servlet;





import com.timeDeal.bean.PinglunBean;
import com.timeDeal.bean.ShijianchushouBean;
import com.timeDeal.bean.UserBean;
import com.timeDeal.bean.XuqiufabuBean;
import com.timeDeal.service.*;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2016/7/24.
 */

public class PinglunlServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, IOException, IOException {
        PinglunServicce ps = new PinglunServicce() ;
        XuqiuService xuqiuService = new XuqiuService() ;
        UserBean ub = (UserBean)request.getSession().getAttribute("USER");
        int id = Integer.valueOf( request.getParameter("id"));
       // String leixing = request.getParameter("leixing");
        String ss =StringUtils.defaultString(request.getParameter("currentpage"),"1");
        String leixing =StringUtils.defaultString(request.getParameter("leixing"),"xuqiufabu");
        int currentpage = Integer.valueOf(ss);
        UserService us = new UserService() ;
        List<String>  name = new ArrayList<>() ;
        List<PinglunBean> pinglun = new ArrayList<>();
        System.out.println(id);
        System.out.println(currentpage);
        System.out.println(leixing);
        if(StringUtils.equals("xuqiufabu",leixing)) {
            XuqiufabuBean xfb = xuqiuService.getAxuquById(id);
            request.setAttribute("xuqiu",xfb);
            request.setAttribute("tijiaoren",xfb.getTijiaorenXingming());
            request.setAttribute("MAX", ps.getCountByLeixingId(leixing,id));
            pinglun =ps.getAllPinglunByLeixingId(Integer.valueOf(id),leixing,currentpage);
            for (int i = 0; i < pinglun.size(); i++) {
                name.add(us.getDetailById(pinglun.get(i).getPinglunrenid()));
            }
            request.setAttribute("name", name);
            for (int s = 0 ;s<pinglun.size();s++) {
                System.out.println(pinglun.get(s).getPinglunneirong());
            }
            request.setAttribute("id",String.valueOf(id));
            request.setAttribute("pinglun" ,pinglun);
            request.setAttribute("currentpage" ,String.valueOf(currentpage));
            request.getRequestDispatcher("/user/pinglun.jsp").forward(request,response);
            }
            else {
            ShijianchusouService shijianchusouService = new ShijianchusouService() ;
            UserService userService = new UserService() ;
            ShijianchushouBean shijianchushouBean = shijianchusouService.getAShijianChushouById(id);
            shijianchushouBean.setTijiaorenXingming(userService.getAuserByid(Integer.valueOf(id)).getYonghuming());
            request.setAttribute("shijian",shijianchushouBean);
            request.setAttribute("tijiaoren",shijianchushouBean.getTijiaorenXingming());
            request.setAttribute("MAX", ps.getCountByShijian(id));
            pinglun =ps.getAllPinglunById(Integer.valueOf(id),currentpage);
            for (int i = 0; i < pinglun.size(); i++) {
                name.add(us.getDetailById(pinglun.get(i).getPinglunrenid()));
            }
            request.setAttribute("name", name);
            for (int s = 0 ;s<pinglun.size();s++) {
                System.out.println(pinglun.get(s).getPinglunneirong());
            }
            request.setAttribute("id",String.valueOf(id));
            request.setAttribute("pinglun" ,pinglun);
            request.setAttribute("currentpage" ,String.valueOf(currentpage));
            request.getRequestDispatcher("/user/shijianchushou/pinglun.jsp").forward(request,response);
        }



    }
}